﻿cmake_minimum_required(VERSION 3.16)

project(SecAssistPro
    VERSION 0.1
    DESCRIPTION "SecAssistPro"
    LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# #### USER DEFINE ####
# 允许qt_add_executable()编译资源文件
set(CMAKE_AUTORCC ON)
# 允许cmake当前目录作为头文件include根路径
set(CMAKE_INCLUDE_CURRENT_DIR ON)

#设置可执行文件输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE})
if (APPLE)
    set(APPLICATION_DIR_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}.app/Contents/MacOS)
else ()
    set(APPLICATION_DIR_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
endif ()

if (MSVC)
    #让Release也生成pdb文件
    set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
endif ()
if (MINGW)
    add_compile_definitions(QT_DEBUG)
    add_compile_options(-g -gdwarf-2)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g")
endif ()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/.cmake/)
include(GetGitRevisionDescription)

# find_package(Qt6 6.2 COMPONENTS Quick REQUIRED)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Quick Network Sql)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Quick Network Widgets Sql)

# 添加国际化脚本
find_program(QT_LUPDATE NAMES lupdate lupdate-qt6)
find_program(QT_LRELEASE NAMES lrelease lrelease-qt6)
file(GLOB TS_FILE_PATHS ${CMAKE_SOURCE_DIR}/res/ *.ts)
add_custom_target(Script-Update-Translations
    COMMENT "Start Run Script-Update-Translations ......"
    COMMAND ${QT_LUPDATE} ${CMAKE_CURRENT_LIST_DIR} -ts res/${PROJECT_NAME}_en_US.ts
    COMMAND ${QT_LUPDATE} ${CMAKE_CURRENT_LIST_DIR} -ts res/${PROJECT_NAME}_zh_CN.ts
    COMMAND ${CMAKE_COMMAND} -E make_directory ${APPLICATION_DIR_PATH}/i18n
    COMMAND ${QT_LRELEASE} res/${PROJECT_NAME}_en_US.ts -qm ${APPLICATION_DIR_PATH}/i18n/${PROJECT_NAME}_en_US.qm
    COMMAND ${QT_LRELEASE} res/${PROJECT_NAME}_zh_CN.ts -qm ${APPLICATION_DIR_PATH}/i18n/${PROJECT_NAME}_zh_CN.qm
    # COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_NAME}_en_US.qm ${PROJECT_NAME}_zh_CN.qm ${APPLICATION_DIR_PATH}/i18n
    SOURCES ${TS_FILE_PATHS}
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
)

# 生成版本信息头文件
set(HEADER_FILE_VERSION_PATH ${CMAKE_SOURCE_DIR}/src/version.h)
configure_file(
    ${CMAKE_SOURCE_DIR}/.cmake/version.h.in
    ${HEADER_FILE_VERSION_PATH}
)

# source
list(APPEND sources_files ${PROJECT_NAME}.qrc)

# 通过模板文件针对windows平台生成rc文件，还有inno setup脚本
set(APP_VERSION_RC_PATH "")
if (WIN32)
    set(APP_VERSION_RC_PATH ${CMAKE_CURRENT_BINARY_DIR}/version_${PROJECT_NAME}.rc)
    configure_file(
        ${CMAKE_SOURCE_DIR}/.cmake/version_exe.rc.in
        ${APP_VERSION_RC_PATH}
    )
    configure_file(
        ${CMAKE_SOURCE_DIR}/.cmake/InstallerScript.iss.in
        ${CMAKE_SOURCE_DIR}/package/InstallerScript.iss
    )
    list(APPEND sources_files ${APP_VERSION_RC_PATH})
endif ()

# list(APPEND sources_files src/main.cpp)
# file(GLOB CPP_FILES src/*.cpp src/*.h)
# file(GLOB CPP_FILES src/common/*.h src/common/*.cpp)
file(GLOB_RECURSE CPP_FILES src/*.cpp src/*.h)
foreach(filepath ${CPP_FILES})
    string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" filename ${filepath})
    list(APPEND sources_files ${filename})
endforeach (filepath)

message(STATUS "sources_files=${sources_files}")
if (${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(${PROJECT_NAME}
        MANUAL_FINALIZATION
        ${sources_files}
    )
else ()
    add_executable(${PROJECT_NAME}
        ${sources_files}
    )
endif ()
add_dependencies(${PROJECT_NAME} Script-Update-Translations)
add_dependencies(${PROJECT_NAME} Copy-Example-File)

# #### Not use ####
# qt_add_qml_module(appSecAssistPro
#     URI SecAssistPro
#     VERSION 1.0
#     QML_FILES qml/main.qml
#     # RESOURCES
# )

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(SecAssistPro PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.SecAssistPro
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

#Release也支持日志打印代码位置
target_compile_definitions(${PROJECT_NAME}
        PRIVATE
        QT_MESSAGELOGCONTEXT
)

target_link_libraries(SecAssistPro
    PRIVATE Qt6::Quick Qt6::Widgets Qt6::Sql)

include(GNUInstallDirs)
install(TARGETS SecAssistPro
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

#复制程序运行所需要的动态库
if (WIN32)
    if (MSVC)
        if (CMAKE_SIZEOF_VOID_P EQUAL 4)
            file(GLOB_RECURSE 3RDPARTY_DLL_DIR ${CMAKE_SOURCE_DIR}/3rdparty/msvc/x86/*.dll)
        elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
            file(GLOB_RECURSE 3RDPARTY_DLL_DIR ${CMAKE_SOURCE_DIR}/3rdparty/msvc/x64/*.dll)
        endif ()
    elseif (MINGW)
        file(GLOB_RECURSE 3RDPARTY_DLL_DIR ${CMAKE_SOURCE_DIR}/3rdparty/mingw/*.dll)
    endif ()
    file(COPY ${3RDPARTY_DLL_DIR} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
endif ()

#拷贝样本文件，用于Debug模式
add_custom_target(Copy-Example-File
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/res/example ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/example
    COMMENT "Copy Example File Done ......"
    SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

#添加部署脚本
message(STATUS "CMAKE_COMMAND=${CMAKE_COMMAND}")
message(STATUS "PROJECT_NAME=${PROJECT_NAME}")
message(STATUS "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
message(STATUS "CMAKE_CURRENT_LIST_DIR=${CMAKE_CURRENT_LIST_DIR}")
message(STATUS "CMAKE_SOURCE_DIR=${CMAKE_SOURCE_DIR}")
message(STATUS "CMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
message(STATUS "APPLICATION_DIR_PATH=${APPLICATION_DIR_PATH}")
if (CMAKE_BUILD_TYPE MATCHES "Release")
    if (APPLE)
        find_program(QT_DEPLOY_QT NAMES macdeployqt)
        add_custom_target(Script-DeployRelease
                COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_SOURCE_DIR}/dist
                COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} ${CMAKE_SOURCE_DIR}/dist
                COMMAND ${QT_DEPLOY_QT} ${CMAKE_SOURCE_DIR}/dist/${PROJECT_NAME}.app -qmldir=${CMAKE_CURRENT_LIST_DIR}
                COMMENT "MacOs Deploying Qt Dependencies After Build........."
                SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        )
    endif ()
    if (WIN32)
        find_program(QT_DEPLOY_QT NAMES windeployqt)
        add_custom_target(Script-DeployRelease ALL
                COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_SOURCE_DIR}/dist
                COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} ${CMAKE_SOURCE_DIR}/dist
                COMMAND ${QT_DEPLOY_QT} --qmldir=${CMAKE_CURRENT_LIST_DIR} --plugindir ${CMAKE_SOURCE_DIR}/dist/plugins --no-translations --compiler-runtime ${CMAKE_SOURCE_DIR}/dist/${PROJECT_NAME}.exe
                COMMENT "Windows Deploying Qt Dependencies After Build........."
                SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                DEPENDS SecAssistPro
        )
    endif ()
endif ()
